C2Rust-Bitfields Crate
This crate is used to generate structs with bitfields in c2rust translations. It has three primary goals:
- Byte compatibility with equivalent C bitfield structs
- The ability to take references/pointers to non bitfield fields
- Provide methods to read from and write to bitfields
We currently provide a single custom derive, BitfieldStruct
, as well as a dependent field attribute bitfield
. The dependent field attribute padding
may optionally be used as a no-op marker for automated tools.
Requirements
- Rust 1.30+
- Rust Stable, Beta, or Nightly
- Little Endian Architecture
Example
Suppose you want to write a super compact date struct which only takes up three bytes. In C this would look like this:
;
Clang helpfully provides us with this information:
*** Dumping AST Record Layout
0 | struct date
0:0-4 | unsigned char day
0:5-8 | unsigned char month
1:1-15 | unsigned short year
|
And this is enough to build our rust struct:
extern crate libc;
Furthermore, C bitfield rules for overflow and signed integers are taken into account.
This crate can generate no_std
compatible code when the no_std
feature flag
is provided.
Tests
Since rust doesn't support a build.rs
exclusively for tests, you must manually compile the c test code and link it in.
$ clang tests/bitfields.c -c -fPIC -o tests/bitfields.o
$ ar -rc tests/libtest.a tests/bitfields.o
$ RUSTFLAGS="-L `pwd`/tests" cargo test
Acknowledgements
This crate is inspired by the rust-bitfield, packed_struct, and bindgen crates.